// pages/scan/scan.js
const app = getApp()
const db = wx.cloud.database()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sign:true,
    login:false,
    openId: "",
    nickName: '',
    avatarUrl: '',
    userInfo:'',
    score:0,
    pass:0,
    time:'',
    newSignBtnState: false,  //按钮签到状态
    myToday: '',           //周几
    newSignNum: 0,      //签到天数
    newSignIntegral:0,  //签到积分
    newSignedArr: [
      {
        "day": "一",
        "isSigned": false,
        "newSignBtnState":false
      },
      {
        "day": "二",
        "isSigned": false,
        "newSignBtnState":false
      },
      {
        "day": "三",
        "isSigned": false,
        "newSignBtnState":false
      },
      {
        "day": "四",
        "isSigned": false,
        "newSignBtnState":false
      },
      {
        "day": "五",
        "isSigned": false,
        "newSignBtnState":false
      },
      {
        "day": "六",
        "isSigned": false,
        "newSignBtnState":false
      },
      {
        "day": "日",
        "isSigned": false,
        "newSignBtnState":false
      }
    ],
  },
  login(){
    console.log(12111)
    if(!this.data.login){
      wx.getUserProfile({
        desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
        success: (res) => {   
          console.log(res)
          wx.setStorage({
            key: "user",
            data: res.userInfo
          })
          this.setData({
            avatarUrl: res.userInfo.avatarUrl,
            nickName: res.userInfo.nickName,
            login: true
          })
          db.collection('users').add({
            data:{
             image_src:this.data.avatarUrl,
             name:this.data.nickName,
             openid:this.data.openId,
             score:0,
             pass:0
            }
          }).then(res=>{
            console.log('success')
          })
          db.collection('sign_list').add({
            data:{
              newSignIntegral:0,
              oldday:0,
              nextday:0,
              arr:this.data.newSignedArr
            }
          })
        } 
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var myDate = new Date();
    var day = myDate.toLocaleDateString()
    console.log(day)
    const user_list = wx.getStorageSync('user_list') || [];
    console.log(user_list)
    this.setData({
      login:true,
      pass:user_list.pass,
      score:user_list.score,
      nickName: user_list.name,
      avatarUrl: user_list.image_src,
      time:user_list.time,
      
    })
    let that = this
    db.collection('sign_list').get()
    .then(res=>{
      that.setData({
        sign:res.data[0].arr[(myDate.getDay)-1].isSigned
      })
    })
    wx.cloud.callFunction({
      name:'openid',
      complete: res => {
        var openId = res.result.event.userInfo.openId
        console.log(openId)
        that.setData({
          openId: openId
        })
        that.queryUser(openId)
      }
    })
    // var mydate = new Date();
    // var year = mydate.getFullYear();
    // var month = mydate.getMonth() + 1;
    // let date = mydate.getDate();
    // console.log("date" + date,year,month)
    // var day = mydate.getDay();
    // console.log(day)
    // var nbsp = 7 - ((date - day) % 7);
    // console.log("nbsp" + nbsp);
  },

   /*  查询用户信息  */
   queryUser(openId) {
    var that = this
    db.collection('users')
    .where({
        openid: openId
      })
    .get({
      success(res) {
        console.log(res)
        wx.setStorage({
          key: "user_list",
          data: res.data[0]
        })
        that.setData({
          login:true,
          pass:res.data[0].pass,
          score:res.data[0].score,
          nickName: res.data[0].name,
          avatarUrl: res.data[0].image_src,
          time:res.data[0].time,
        })
      }
    })
  },
  //每日签到函数
  sign(){
    var myDate = new Date();
    var day = myDate.toLocaleDateString()
    // if(day !== this.data.time){
    //   wx.navigateTo({
    //   url: '../../pages/qiandao/index',
    // })
    // }else{
    //   wx.navigateTo({
    //     url: '../../pages/advice/index',
    //   })
    // }
    wx.navigateTo({
      url: '../../pages/qiandao/index',
    })
    
  },
  //跳转到意见页面
  advice(){
    wx.navigateTo({
      url: '../../pages/advice/index',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(this.data.login){
      const user_list = wx.getStorageSync('user_list') || [];
      this.setData({
        score:user_list.score
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})